package org.itboys.mobile.service.mongo.mobile;


import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.util.ArrayListWrapper;
import org.itboys.commons.utils.common.DistanceUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.product.SundayProductMapper;
import org.itboys.mobile.dto.SundayMemberDto;
import org.itboys.mobile.entity.mongo.SundayCid;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：平安车行_商户_service
 */
@Service
public class SundayMobileBusinessService  {
    @Autowired
    private SundayMobileMemberService memberService;
    @Autowired
    private SundayProductMapper productMapper;
    @Autowired
    private ResourceHolder resourceHolder;
    //获取商户列表（按经纬度排序）
    public List<SundayMemberDto> getBusiness(HttpServletRequest request,Integer number,Long memberId,String categoryId){
            List<SundayMemberDto> businessDtos = new ArrayList<SundayMemberDto>();
             List<SundayMember> businesses =null;
            Map<String,Object> param = null;
            Map<String,String> containsparam = null;
            if(request!=null){
                param= ParamUtil.packageMongoExactParam(request,"");
                containsparam= ParamUtil.packageMongoVagueParam(request,"name");
               // businesses=memberService.containpageQuery(request,containsparam,param).getData();
            }else{
                param = new HashedMap();
                if(StringUtils.isNotEmpty(categoryId)){
                    param.put("contains:categoryId",categoryId);
                }

            }
            param.put("userType",SundayCommonConstants.user_type_business);
            if(number!=null){
                businesses=memberService.list(param,number);
            }else{
                businesses=memberService.list(param);
            }
            if(businesses==null)return businessDtos;
            //获取用户的经纬度
            SundayMember member = memberService.getLocation(memberId);
            Double memberLongitude = member.getLongitude();
            Double memberLatitude = member.getLatitude();


          //查询所有的套餐
            ///List<Long> productIds = new ArrayList<Long>();
            List<Long> businessIds=new ArrayList<Long>();
            for(SundayMember business:businesses){
                businessIds.add(business.getId());
            }
            param.clear();
        List<SundayProduct> products =new ArrayList<SundayProduct>();
        if(ListUtil.isNotNull(businessIds)){
            param.clear();
            param.put("memberIds",businessIds);
            param.put("status", SundayCommonConstants.NO);
            param.put("type",SundayCommonConstants.type_mall);
            products=productMapper.select(param);
        }
        //组装DTO
        for(SundayMember business:businesses){
              SundayMemberDto businessDto=memberService.packageDtoSingle(business);
              //组装商品

                List<Map<String,Object>> productMaps = new ArrayList<Map<String,Object>>();
                for(SundayProduct product:products){
                    if(LongUtil.isNotZreo(product.getMemberId())&&
                            business.getId()==product.getMemberId().longValue()){
                        Map<String,Object> productMap = new HashMap<String, Object>();
                        productMap.put("id",product.getId());
                        productMap.put("name",product.getName()+product.getStandard());
                        productMap.put("image",resourceHolder.getStringValue("imgRoot")+product.getImage());
                        productMap.put("price",product.getPrice());
                        productMap.put("marketPrice",product.getMarketPrice());
                        productMaps.add(productMap);
                        //不要break;
                    }
                }

                        businessDto.setProducts(productMaps);
                        //计算距离
                        businessDto.setDistance(DistanceUtils.GetDistance(memberLongitude,memberLatitude,business.getLongitude(),business.getLatitude()));
                        businessDtos.add( businessDto);
        }
        //按距离排序
        Collections.sort(businessDtos, new Comparator<SundayMemberDto>() {
            @Override
            public int compare(SundayMemberDto memberDto1,SundayMemberDto memberDto2) {
                Double distence1= memberDto1.getDistance();
               // Long id1=(Long)map1.get("id");
                Double distence2=  memberDto2.getDistance();
              //  Long id2=(Long)map2.get("id");
                //如果距离大于0
                if(distence1.compareTo(distence2)==1){
                    return distence1.compareTo(distence2);
                }
                return -1;
            }
        });
        return businessDtos;
    }


}
